package com.to8to.kitt.esb.http;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.util.Date;

import org.apache.thrift.protocol.TMessageMeta;

import com.to8to.kitt.ThriftMessage;
import com.to8to.kitt.esb.BusMessage;

public class HttpBusMessageState {

	public String key;
	
	public int oldseq;
	
	public int newseq;
	
	public long recvFromClientTime = System.currentTimeMillis();

	public long startSendDownTime;

	public long finishSendDownTime;
	
	public long recvFromDownTime;
	
	public long startSendClientTime;
	
	public long finishSendClientTime;
	
	public Channel downpool;
	
	public ChannelHandlerContext upctx;
	
	public TMessageMeta meta;
	
	public ThriftMessage inMsg;
	
	public ThriftMessage outMsg;
	
	public HttpBusMessageState(String key, int oldseq, int newseq, TMessageMeta meta, ChannelHandlerContext upctx, ThriftMessage inMsg)
	{
		this.key = key;
		
		this.oldseq = oldseq;
		
		this.newseq = newseq;
	
		this.meta = meta;
		
		this.upctx = upctx;
		
		this.inMsg = inMsg;
	}
	
	
	

	public void setRecvFromClientTime(long recvFromClientTime) {
		this.recvFromClientTime = recvFromClientTime;
	}



	public int getOldseq() {
		return oldseq;
	}



	public void setOldseq(int oldseq) {
		this.oldseq = oldseq;
	}



	public int getNewseq() {
		return newseq;
	}



	public void setNewseq(int newseq) {
		this.newseq = newseq;
	}



	public String getKey() {
		return key;
	}



	public void setKey(String key) {
		this.key = key;
	}



	public TMessageMeta getMeta() {
		return meta;
	}



	public void setMeta(TMessageMeta meta) {
		this.meta = meta;
	}



	public void setUpctx(ChannelHandlerContext upctx) {
		this.upctx = upctx;
	}



	public ChannelHandlerContext getUpctx() {
		return upctx;
	}



	public Channel getDownpool() {
		return downpool;
	}



	public void setDownpool(Channel downpool) {
		this.downpool = downpool;
	}


	public void recordStartSendDownTime()
	{
		this.startSendDownTime = System.currentTimeMillis();
	}
	
	public void setRecvFromDownTime(long recvFromDownTime) {
		this.recvFromDownTime = recvFromDownTime;
	}



	public void recordRecvFromDownTime()
	{
		this.recvFromDownTime = System.currentTimeMillis();
	}
	public void recordFinishSendDownTime()
	{
		this.finishSendDownTime = System.currentTimeMillis();
	}
	public void recordFinishSendClientTime()
	{
		this.finishSendClientTime = System.currentTimeMillis();
	}
	
	public void recordStartSendClientTime()
	{
		this.startSendClientTime = System.currentTimeMillis();
	}
	
	public long getTotalTime()
	{
		return finishSendClientTime - recvFromClientTime;
	}

	public long getRecvFromClientTime() {
		return recvFromClientTime;
	}

	public long getStartSendDownTime() {
		return startSendDownTime;
	}



	public long getFinishSendDownTime() {
		return finishSendDownTime;
	}



	public long getRecvFromDownTime() {
		return recvFromDownTime;
	}



	public long getFinishSendClientTime() {
		return finishSendClientTime;
	}



	public String getTag()
	{
		if(meta == null)
			return null;
		
		return meta.tag;
	}
	
	public String getName()
	{
		if(meta == null)
			return null;
		if(meta.tag != null)
			return meta.service + ":" + meta.method + ":" + meta.tag;
		return meta.service + ":" + meta.method;
	}
	
	public String spendTimeString()
	{
		StringBuilder sb = new StringBuilder();
		sb.append("跟下游路由以及建立连接耗时:" + (startSendDownTime - recvFromClientTime) + "ms, ");
		sb.append("发送下游数据包耗时:" + (finishSendDownTime - startSendDownTime) + "ms, ");
		sb.append("等待下游响应以及接收数据包耗时:" + (recvFromDownTime - finishSendDownTime) + "ms, ");
		sb.append("发送客户端数据包耗时:" + (finishSendClientTime - recvFromDownTime) + "ms, ");
		sb.append("总耗时:" + (finishSendClientTime - recvFromClientTime) + "ms, ");
		sb.append("总线处理两次handle耗时:" + (startSendDownTime - recvFromClientTime + startSendClientTime - recvFromDownTime) + "ms");
		return sb.toString();
	}
	

	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		
		sb.append("-----------------------\n");
		sb.append("oldSeqId:" + oldseq + "\t");
		sb.append("newSeqId:" + newseq+ "\t");
		sb.append("recvFromClientTime:" + new Date(recvFromClientTime)+ "\t");
		sb.append("startSendDownTime:" + new Date(startSendDownTime)+ "\t");
		sb.append("finishSendDownTime:" + new Date(finishSendDownTime)+ "\t");
		sb.append("recvFromDownTime:" + new Date(recvFromDownTime)+ "\t");
		sb.append("finishSendClientTime:" + new Date(finishSendClientTime)+ "\t");
		sb.append("downChannel:" + downpool+ "\t");
		sb.append("upChannel:" + upctx.channel()+ "\t");
		sb.append("\n-----------------------");
		
		return sb.toString();
		
	}
}
